import os
import re
import pandas as pd

from utils import LOG
from context import CONTEXT

class SeedDataLoader:
    """
    DataLoader class is responsible for loading data from several given files:
    1. load seed data from the files under seed_data_files folder into corresponding pandas DataFrame
    """
    def load_data(self):
        # Load seed data from the files under seed_data_files folder
        seed_data = {}
        seed_data_files_path = os.path.join(CONTEXT.get('common-paths').get('generator-source-files-path'), CONTEXT.get('zip_filename'), 'seed_data_files')
        # iterate over the csv files in the seed_data_files_path folder
        for seed_data_filename in os.listdir(seed_data_files_path):
            seed_data_path = os.path.join(seed_data_files_path, seed_data_filename)
            # check the name of csv file matches the pattern '{app}.{db}.{table}.csv'
            if not re.match(r'\w+\.\w+\.\w+\.csv', seed_data_filename):
                LOG.error(f'Invalid seed data file: {seed_data_filename}')
                return
            seed_data_df = pd.read_csv(seed_data_path)
            filename, _ = os.path.splitext(seed_data_filename)
            seed_data[filename] = seed_data_df
        
        return seed_data
